﻿using FireBillBook.Core.Entities;
using FireBillBook.Core.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using FireBillBook.Core.Models.QueryContexts;
using System.Text;
using System;

namespace FireBillBook.Core.Repos
{
    public class BillingSourceDataRepo : Interfaces.IBillingSourceDataRepo
    {
        private BillDbContext _db;

        public BillingSourceDataRepo(BillDbContext billDbContext)
        {
            _db = billDbContext;
        }

        public async Task BulkSaveAsync(List<BillingSourceData> datas, string platform)
        {
            var tradingCodes = datas.Select(a => a.TradingCode).ToList();
            var platformStr = platform.ToString();

            // 已存在数据
            var existsRecords = await _db.BillingSourceDatas.AsQueryable()
                .Where(a => a.DataSourceCode.Equals(platformStr) && tradingCodes.Contains(a.TradingCode))
                .ToListAsync();

            List<BillingSourceData> addRecords;
            // 增加的记录
            if (existsRecords.Count > 0)
            {
                var existsTradingCodes = existsRecords.Select(a => a.TradingCode).ToList();

                addRecords = datas.Where(a => !existsTradingCodes.Exists(b => b == a.TradingCode) && a.DataSourceCode.Equals(platformStr)).ToList();

                foreach (var r in existsRecords)
                {
                    var newData = datas
                        .Where(a => a.DataSourceCode.Equals(r.DataSourceCode) && a.TradingCode.Equals(r.TradingCode))
                        .FirstOrDefault();

                    if(newData == null)
                    {
                        continue;
                    }

                    r.Amount = newData.Amount;
                    r.ExternalData = newData.ExternalData;
                    r.Remark = newData.ExternalData;
                }
            }
            else
            {
                addRecords = datas;
            }

            foreach (var r in addRecords)
            {
                _db.BillingSourceDatas.Add(r);
            }

            await _db.SaveChangesAsync();
        }

        public async Task<PagingModel<BillingSourceData>> ListAsync(BillingSourceDataListQueryContext queryContext)
        {
            var query = _db.BillingSourceDatas.AsNoTracking();

            if (!string.IsNullOrEmpty(queryContext.Platform))
            {
                query = query.Where(a => a.DataSourceCode.Equals(queryContext.Platform));
            }

            if (queryContext.Start != null)
            {
                query = query.Where(a => a.TradingTime.Date.CompareTo(queryContext.Start.Value.Date) >= 0);
            }

            if (queryContext.End != null)
            {
                query = query.Where(a => a.TradingTime.Date.CompareTo(queryContext.End.Value.Date) <= 0);
            }

            var paging = queryContext.ConvertToPagingModel<BillingSourceData>();

            paging.Total = await query.CountAsync();

            query = queryContext.TryPaging(query.OrderBy(a=>a.TradingTime));

            paging.Collection = await query.ToListAsync();

            return paging;
        }
    }
}
